package risk.control.project.api;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import risk.control.project.entity.repository.UserBean;
import risk.control.project.framework.PostController;
import risk.control.project.framework.entity.TokenBean;
import risk.control.project.service.UserService;
import risk.control.project.utils.response.ResponseCode;

import java.util.HashMap;


@RestController
@RequestMapping(value = "/main", produces = {"application/json;charset=UTF-8"})
public class MAIN_000_000_005 extends PostController {
    private static final Logger logger = LoggerFactory.getLogger(MAIN_000_000_005.class);

    private final UserService userService;

    //查询用户信息状态接口-（通常用于移动端的免密登录）
    public MAIN_000_000_005(UserService userService) {
        this.userService = userService;
    }

    @PostMapping(value = "/000_000_005")
    public String doPost(HttpServletRequest request) {
        return handleRequest(request);
    }

    @Override
    protected String postRequest(HashMap<String, String> param) {
        String requestBody = param.get("requestBody");
        /*
         * 1、这里不用验证token有效性，因为基类处理好了
         * 2、用于免密登录(移动端)，获取用户基础信息
         */
        //解析token，根据userid查库获取用户信息
        JSONObject jsonObject = (JSONObject) JSON.parse(requestBody);
        String token = jsonObject.getString("token");
        TokenBean tokenBean = tokenService.decryptToken(token);
        String userId = tokenBean.userId;
        if (userId == null || userId.isEmpty()) {
            return generateFailForCode(ResponseCode.TOKEN_ERROR, "token解析异常");
        }
        //根据userid查询用户信息
        logger.warn("用户id==》" + userId);
        UserBean userBean = userService.getUserForId(userId);
        if (userBean == null) {
            return generateFail("获取用户信息异常");
        }
        //封装响应体
        HashMap<String, String> resMap = new HashMap<>();
        resMap.put("userName", userBean.userName);
        resMap.put("branName", userBean.branchName);
        resMap.put("belongDistricts", userBean.belongDistricts);
        resMap.put("branchAddress", userBean.branchAddress);
        resMap.put("bankName", userBean.bankName);
        return generateSucceed(resMap);
    }
}
